package org.erepublik.company.controllers;

import java.util.ArrayList;
import java.util.Arrays;

import org.erepublik.company.comparators.FoodComparatorByWellness;
import org.erepublik.company.log.SitesLogger;
import org.erepublik.company.product.Food;
import org.erepublik.company.searchCriteria.EAllianceSearchCriteria;
import org.erepublik.company.searchCriteria.EQualityFilter;
import org.erepublik.company.searchCriteria.FoodSearchCriteria;
import org.erepublik.company.service.IProductsService;
import org.erepublik.company.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Kontroler odpowiedzialny na wyszukiwanie najtanszego jedzenia
 *
 * @author Kacper Gorski 'Railman' railman85@gmail.com
 */
@Controller
@RequestMapping("/food.html")
public class FoodController extends AbstractController {

    @Autowired // autumatycznie wstrzykiwany obiekt
    private SitesLogger sitesLogger; 
    
	@Autowired // autumatycznie wstrzykiwany obiekt
	private IProductsService productService; 
	
	@RequestMapping(method = RequestMethod.GET)
	public String showForm(ModelMap model) {
        fillModelMap(model);
	    Food[] products = productService.getFood(null);
		Arrays.sort(products, new FoodComparatorByWellness());
		products = shrinkTable(products);
		model.addAttribute("updateTime", productService.getMinutesSinceFoodUpdate() );
		model.addAttribute("products", products);
		model.addAttribute("foodSearchCriteria", new FoodSearchCriteria());
		sitesLogger.log("FOOD", "Country: null SortCritera: Wellness Quality: null");
		return "food";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String showList(ModelMap model, @ModelAttribute("foodSearchCriteria") FoodSearchCriteria criteria) {
        fillModelMap(model);
	    Food[] products = productService.getFood(criteria.getCountry());
	    products = filterAlliances(criteria.getAlliance(), products);
		Arrays.sort(products, new FoodComparatorByWellness());
		if (criteria.getQualityFilter()!=EQualityFilter.ALL)
		    products = qualityFilter(products, criteria.getQualityFilter());
		products = shrinkTable(products);
		model.addAttribute("updateTime", productService.getMinutesSinceFoodUpdate() );
		model.addAttribute("products", products);
		sitesLogger.log("FOOD", "Country: "+criteria.getCountry()
		
		        +" Quality: "+criteria.getQualityFilter());
		return "food";
	}
	
	
	
    private Food[] filterAlliances(EAllianceSearchCriteria alliance, Food[] products) {

        ArrayList<Food> list = new ArrayList<Food>();
        for (Food food : products) {
            if ( Utils.contains( food.getCountry().getAlliance(), alliance.getAlliances() ) )
                list.add(food);
        }
        return list.toArray(new Food[0]);
    }

    
    
    private Food[] qualityFilter(Food[] products, EQualityFilter filter) {
        ArrayList<Food> weapons = new ArrayList<Food>();
        for (Food house : products) {
            if (house.getQuality() == filter.getQuality())
                weapons.add(house);
        }
        return weapons.toArray(new Food[0]);
    }
	
    
    final int MAX_SIZE=50; // maksymalna ilosc wyswietlanych produktow
    // skraca tabele do MAX_SIZE produktow
    private Food[] shrinkTable(Food[] products) {
        if (products.length <= MAX_SIZE)
            return products;
        else {
            Food[] newTable = new Food[MAX_SIZE];
            for (int i=0; i < MAX_SIZE; i++) {
                newTable[i]=products[i];
            }
            return newTable;
        }
    }
	
	
}
